<?php
class PdoMysqlEngine
{
	private $connection;
	
	public function __construct($db_config)
	{
		$this->connection = $this->getConnection($db_config);
	}
	
	public function delete($table_name,$where='')
	{
		$sql = 'DELETE FROM `'.$table_name.'`';
		if(!empty($where))
		{
			$sql .= ' WHERE '.MysqliEngine::implodeToWhere($where);
		}
		$result = $this->connection->exec($sql);
		if($result===false)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	public function getConnection($db_config)
	{
		$str = 'mysql:host='.$db_config['host'].';port='.$db_config['port'].';dbname='.$db_config['db_name'];
		try
		{
			$connection = new PDO($str, $db_config['username'], $db_config['password']);
			$connection->exec('SET NAMES \'' . $db_config['encoding'].'\';');
			return $connection;
		}
		catch(PDOException $e)
		{
			throw new Exception($connection->errorCode());
		}
	}
	
	public function insertRow($table_name,$data)
	{
		$sql = 'INSERT INTO `'.$table_name.'` ('.MysqliEngine::implodeToColumn(array_keys($data)).') VALUES '.MysqliEngine::implodeToRowValues(array_values($data));
		$result = $this->connection->exec($sql);
		if($result===false)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	public function insertRows($table_name,$data)
	{
		$column_name_array = array();
		$data_for_query = array();
		$column_name_array = array_keys($data[0]);
		foreach($data as $one_row)
		{
			$data_for_query[] = array_values($one_row);
		}
		$sql = 'INSERT INTO `'.$table_name.'` ('.MysqliEngine::implodeToColumn(array_values($column_name_array)).') VALUES '.MysqliEngine::implodeToRowsValues($data_for_query);
		$result = $this->connection->exec($sql);
		if($result===false)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	public function selectCount($table_name,$where='')
	{
		$sql = 'SELECT COUNT(*) FROM `'.$table_name.'`';
		if(!empty($where))
		{
			$sql .= ' WHERE '.MysqliEngine::implodeToWhere($where);
		}
		$result = $this->connection->query($sql);
		return $result->fetchColumn();
	}
	
	public function selectRow($table_name,$where='',$column='*',$limit_start=0,$group_by='',$order_by='')
	{
		$result = $this->selectRows($table_name,$where,$column,$limit_start,1,$group_by,$order_by);
		if($result===false)
		{
			return false;
		}
		else
		{
			return empty($result) ? array() : $result[0];
		}
	}
	
	public function selectRows($table_name,$where='',$column='*',$limit_start=0,$limit_size='',$group_by='',$order_by='')
	{
		
		$sql = 'SELECT '.MysqliEngine::implodeToColumn($column).' FROM `'.$table_name.'`';
		if(!empty($where))
		{
			$sql .= ' WHERE '.MysqliEngine::implodeToWhere($where);
		}
		$sql = !empty($group_by) ? $sql.' GROUP BY '.$group_by : $sql;
		$sql = !empty($order_by) ? $sql.' ORDER BY '.$order_by : $sql;
		if(!empty($limit_size))
		{
			$sql .= ' LIMIT '.$limit_start.','.$limit_size;
		}
		$result = $this->connection->query($sql);
		$result->setFetchMode(PDO::FETCH_ASSOC);
		return $result->fetchAll();
	}
	
	public function update($table_name,$where,$data)
	{
		$sql = 'UPDATE `'.$table_name.'` SET '.MysqliEngine::implodeToUpdate($data);
		if(!empty($where))
		{
			$sql .= ' WHERE '.MysqliEngine::implodeToWhere($where);
		}
		$result = $this->connection->exec($sql);
		if($result===false)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}
?>